from email.policy import strict
import requests
from lxml import etree
import os
import stat
import urllib.request
import sys
# from seed import main


class Down:
    def __init__(self):

        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
        }
        for page in range(0, 250, 25):
            url = 'https://movie.douban.com/top250?start=%s&filter=' % page
            self.get_page(url)

    def get_page(self, url):
        res = self.common_get(url)
        self.handle_res(res)

    def common_get(self, url):
        res = requests.get(url, headers=self.headers)
        res.encoding = "utf8"
        response = etree.HTML(res.text)
        return response

    def handle_res(self, res):
        # 获取标题
        lis_item = res.xpath(
            '//ol[@class="grid_view"]//li//div[@class="item"]')
        # print(lis)
        print(lis_item)
        for item in lis_item:
            # url = item.xpath(
            #     './/div[@class="hd"]/a/@href')
            # print(url)
            text = item.xpath(
                './/div[@class="bd"]//span[@class="inq"]/text()')
            idx = item.xpath('.//div[@class="pic"]/em/text()')

            title = item.xpath(
                './/div[@class="hd"]/a/span[1]/text()')
            if len(title) > 0:
                str_con = "<%s>【%s】" % (idx[0], title[0]) 
                if len(text) > 0:
                    str_con += text[0]
                print(str_con)
                # main(1, str_con)
                with open('top250.txt', 'a', encoding='utf-8') as file:
                    file.write(str_con + '\n')

            # print(text)

            # item_res = self.common_get(url)
            # title = item_res.xpath('//h1/span/text()')
            # print(title)
        # text_list = res.xpath(
        #     '//ol[@class="grid_view"]//li//span[@class="inq"]//text()')
        # print(text_list)


Down()
